<template>
    <el-dialog
            :title="title"
            :visible.sync="isVisible"
            :width="width"
            :before-close="handleClose"
            :lock-scroll="true"
            :close-on-click-modal="false"
            custom-class="pub_dialog"
            @open="getData"
    >
        <VForm
                ref="vformComponents"
                :form="formData.form"
                :options="formData.options"
                :rules="formData.rules"
                :labelWidth="formData.labelWidth"
                :rowspan="rowspan"
        ></VForm>

        <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="sure">确 定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    import { warnRecordReportDetail,
            warnRecordReportUpdate,
            warnRecordReportAdd } from '@/api/risk/warnReport'
    import VForm from "@/components/VForm/index";
    export default {
        components: {
            VForm,
        },
        props: {
            width: {
                type: String,
                default: "70%",
            },
            title: {
                type: String,
                default: "详情",
            },
            id:{},
            openType:{
                type:String,
                default:"add"
            },
            dialogVisible: {
                type: Boolean,
                default: false
            },
            alarmBclassifyOptions:{},
            alarmSclassifyOptions:{},
            alarmLevelOptions:{}
        },
        watch: {
            'width': {
                handler(newVal, oldVal) {
                    console.log('newVal===',newVal, oldVal)
                    let w = this.width.split('%')[0]
                    if(w>=60){
                        this.rowspan = 2
                    }else{
                        this.rowspan = 1
                    }
                },
                deep: true,
                immediate: true
            },
            'alarmBclassifyOptions':{
                handler(newVal, oldVal) {
                      this.alarmBclassifyOptionsFormat = []
                       let temp = []
                        newVal.forEach(t=>{
                           let nameList =  t.name.split(",")
                           nameList.forEach(x=>{
                             temp.push({label:t.label, value:x})
                           })
                        })
                       this.alarmBclassifyOptionsFormat = temp
                       console.log(" this.alarmBclassifyOptionsFormat ===", this.alarmBclassifyOptionsFormat )
                       console.log(" this.alarmBclassifyOptions ===", this.alarmBclassifyOptions )
                },
                deep: true,
                immediate: true
            },
            'alarmBclassifyOptionsFormat':{
              handler(newVal, oldVal) {
                this.formData.options.forEach(t=>{
                  if(t.key=='yjdl'){
                    t.option.data = newVal
                  }
                })
              },
              deep: true,
              immediate: true
            },
        },
        computed: {
            isVisible: {
                get() {
                    return this.dialogVisible
                },
                set() {
                    this.close()
                }
            }
        },
        data() {
            return {
                alarmBclassifyOptionsFormat:[],
                rowspan:1,
                dict:{},
                formData: {
                    form: {
                            csjs:"",
                            tjyf:"",
                            yjdl:"",
                            yjxl:"",
                            bjdj:"",
                            tjpf:"",
                            dybjgs:"",
                            bjfxbg:[],
                        },
                    options: [
                                   {
                                       title: "城市简述",
                                       key: "csjs",
                                       type: "text",
                                       disabled: true
                                   },
                                   {
                                       title: "统计月份",
                                       key: "tjyf",
                                       type: "text",
                                       disabled: true
                                   },
                                   {
                                       title: "预警大类",
                                       key: "yjdl",
                                       type: "select",
                                       multiple:false,
                                       option: {
                                         data: this.alarmBclassifyOptionsFormat,
                                         props: {
                                           key: "value", // 指定key对应data的字段
                                           label: "label", // 指定label对应data的字段
                                           value: "value", // 指定value对应data的字段
                                         },
                                       },
                                       disabled: true
                                   },
                                   {
                                       title: "预警细类",
                                       key: "yjxl",
                                       type: "select",
                                       multiple:false,
                                       option: {
                                         data: this.alarmSclassifyOptions,
                                         props: {
                                           key: "id", // 指定key对应data的字段
                                           label: "label", // 指定label对应data的字段
                                           value: "name", // 指定value对应data的字段
                                         },
                                       },
                                       disabled: true
                                   },
                                   {
                                       title: "报警等级",
                                       key: "bjdj",
                                       type: "select",
                                       multiple:false,
                                       option: {
                                         data: this.alarmLevelOptions,
                                         props: {
                                           key: "id", // 指定key对应data的字段
                                           label: "label", // 指定label对应data的字段
                                           value: "value", // 指定value对应data的字段
                                         },
                                       },
                                      disabled: true
                                   },
                                   {
                                       title: "当月报警个数",
                                       key: "dybjgs",
                                       type: "text",
                                       disabled: true
                                   },
                                   {
                                     title: "统计评分",
                                     key: "tjpf",
                                     type: "text",
                                   },
                                   {
                                       title: "报警分析报告",
                                       key: "bjfxbg",
                                       type: "file",
                                       fileType:"file",
                                       listType:"text",
                                       uploadTextTip:""
                                   },
                    ],
                    rules: {
                        csjs :[
                          {max:36, message: '长度最大为36个字符', trigger: 'blur'}
                        ],
                        tjyf :[
                        ],
                        yjdl :[
                          {max:16, message: '长度最大为16个字符', trigger: 'blur'}
                        ],
                        yjxl :[
                          {max:16, message: '长度最大为16个字符', trigger: 'blur'}
                        ],
                        bjdj :[
                        ],
                        tjpf :[
                        ],
                        dybjgs :[
                        ],
                        bjfxbg :[
                        ],
                    },
                    labelWidth: "100px",
                },
            // 字典
            };
        },
        methods: {
            submitFormat(){
                   this.formData.form.tjyf = null
                   this.formData.form.TJYF = null
                   this.formData.form.bjfxbg = JSON.stringify(this.formData.form.bjfxbg)
            },
            loadFormat(){
                    this.formData.form.bjfxbg  =  this.formData.form.bjfxbg ? JSON.parse(this.formData.form.bjfxbg) :[]
            },
            getCheckBoxData(value,option,type){
                if(type=="submit"){
                    if(value.length==0){
                        return null
                    }
                    let temp = [];
                    value.forEach(t=>{
                        let item = option.find(x=> x.label == t)
                    if(item!=undefined){
                        temp.push(item.value)
                    }
                })
                    return temp.join(",")
                }else {
                    if(!value){
                        return []
                    }else {
                        let data = value.split(",")
                        let temp = []
                        data.forEach(t=>{
                            let item = option.find(x=> x.value == t)
                        if(item!=undefined){
                            temp.push(item.label)
                        }
                    })
                        return temp
                    }
                }
            },
            sure(){
                console.log('sure===',this.formData.form)
                this.$refs.vformComponents.$refs.form.validate().then(res=>{
                    this.submitFormat()
                    if(this.openType == "add"){
                        this.add()
                    }else{
                        this.update()
                    }
                })
            },
            add(){
                this.formData.form.id = null
                warnRecordReportAdd(this.formData.form).then(res=>{
                    if(res.code==200){
                    this.$message({
                        message: this.$t('tips.createSuccess'),
                        type: 'success'
                    })
                    this.closeSuccess()
                    }else{
                    this.$message({
                        message: this.$t('tips.systemException'),
                        type: 'error'
                    })
                    }
                })
            },
            update(){
                   warnRecordReportUpdate(this.formData.form).then(res=>{
                        if(res.code==200){
                        this.$message({
                             message: this.$t('tips.updateSuccess'),
                            type: 'success'
                        })
                        this.closeSuccess()
                      }else{
                        this.$message({
                            message: this.$t('tips.systemException'),
                            type: 'error'
                        })
                     }
                })
            },
            cancel(){
                this.close()
            },
            handleClose(done) {
                done()
                this.close()
            },
            closeSuccess(){
                this.$refs.vformComponents.$refs.form.resetFields();
                this.$emit('success')
            },
            close(){
                this.$refs.vformComponents.$refs.form.resetFields();
                this.$emit('close')
            },
            getData(){
                if(this.openType!="add"){
                    warnRecordReportDetail(this.id).then(res=>{
                        if(res.code==200){
                            this.formData.form = res.data
                            this.formData.form.yjdl = res.data.yjxl
                            this.loadFormat()
                         }else{
                            this.$message({
                                message: this.$t('tips.systemException'),
                                type: 'error'
                            })
                        }
                    })
                }
            }
        },
    };
</script>

<style lang="scss" scoped>

</style>
